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MESS4
	CALL	PRINTMESS
	EI
	LD	B,200
MEEE	HALT
	DJNZ MEEE
	CALL	GETKBD
	JP	MENU
BREAK4
	POP	HL
BREAK3
	POP	HL
	JR	BREAK
;
READLN
	LD	HL,#C000
READLN1
	CALL	GETKBD
	CP	#0D
	JR	Z,READLN_CR
	CP	12
	JR	Z,READLN_BS
	CP	#20
	JR	C,READLN1
	LD	(HL),A
	INC	HL
	CALL	PRINT64
	JR	READLN1
READLN_CR
	LD	(HL),A
	INC	HL
	LD	(HL),#1A
	JP	PRINT64
READLN_BS
	LD	A,L
	OR	A
	JR	NZ,READLN_BS1
	LD	A,H
	CP	#C0
	JR	Z,READLN1
READLN_BS1
	DEC	HL
	PUSH	HL
	LD	A," "
	CALL	PRINT_CHAR
;
	LD	HL,AT_X
	LD	A,(HL)
	AND	A
	JR	Z,CUR0
	DEC	(HL)
CONTCUR
	LD	A,8
	CALL	PRINT_CHAR
CON2
	POP	HL
	JR	READLN1
;
CUR0
	LD	(HL),63
	DEC	HL
	DEC	(HL)
	LD	A,(HL)
	AND	A
	JR	Z,CON2
	JR	CONTCUR
;
TXBLOCK
	DI
	LD	HL,#C000
	LD	B,16
T1
	LD	A,#33
	OUT	(#F7),A
	XOR	A
	CALL	TXBYTE
	DJNZ	T1
;
TXBLOCK1
	LD	A,#33
	OUT	(#F7),A
	LD	A,(HL)
	CALL	TXBYTE
	INC	HL
	CP	#1A
	JR	NZ,TXBLOCK1
	CALL	TXBYTE
	CALL	TXBYTE
	CALL	TXBYTE
	CALL	TXBYTE
	CALL	TXBYTE
	CALL	TXBYTE
	CALL	TXBYTE
	EI
	RET
;
TXBYTE
	PUSH	AF
TXBYTE1
	CALL	#1F54
	JP	NC,BREAK3
	IN	A,(#F7)
	BIT	0,A
	JR	Z,TXBYTE1
	POP	AF
	OUT	(#77),A
	RET
GETKBD
	EI
	RES	5,(IY+1)
GETKBD1
	BIT	5,(IY+1)
	JR	Z,GETKBD1
	CALL	#1F54
	JP	NC,BREAK4
	LD	A,(IY-50)
	RES	5,(IY+1)
	RET
;
MESS1
	DEFB	"--------------------------------",13
	DEFB	"Welcome to SPECCY/AMIGA BBS v1.0",13
	DEFB	"Copyright 1994	by Rst7	& (R)soft",13
	DEFB	"BBS phone is 110564 (Dima)",13
	DEFB	"--------------------------------",13
	DEFB	"Press <C> to CONNECT or <Q> to	Quit",13
	DEFB	13,0
MESS2
	DEFB	13
	DEFB	"You are connected...",13
	DEFB	"--------------------",13,0
MESS3
	DEFB	13,"Good bye...",13,0
MESS4
	DEFB	13
	DEFB	"------	 B R E A K  -------"
	DEFB	13
	DEFB	"You have been disconnecting...",13
	DEFB	"Press any key to return.",13
	DEFB	0
;
PRINTMESS
	LD	A,(HL)
	AND	A
	RET	Z
	CALL	PRINT64
	INC	HL
	JR	PRINTMESS
;
PRINT64
	PUSH	AF
	PUSH	BC
	PUSH	DE
	PUSH	HL
	CP	13
	JR	Z,ENTER
	CALL	PRINT_CHAR
	LD	HL,AT_X
	INC	(HL)
	BIT	6,(HL)
	JR	Z,NO_NEXT_AT_Y
ENTER2
	LD	HL,AT_X
	LD	(HL),0
	DEC	HL
	INC	(HL)
;
	LD	A,(HL)
	CP	24
	JR	C,NO_NEXT_AT_Y
	DEC	(HL)
	LD	BC,#1700
	CALL	SCROLL
NO_NEXT_AT_Y
	LD	A,8
	CALL	PRINT_CHAR
	POP	HL
	POP	DE
	POP	BC
	POP	AF
	RET
;
ENTER
	LD	A," "
	CALL	PRINT_CHAR
	JR	ENTER2
;
PRINT_CHAR
	SRL	A
	PUSH	AF
	LD	H,0
	LD	L,A
	ADD	HL,HL
	ADD	HL,HL
	ADD	HL,HL
	LD	DE,FONT
	ADD	HL,DE
	EX	DE,HL
	CALL	ADDR_OF_OUT64
	LD	B,8
	JR	C,PRINT_IN_RIGHT
	POP	AF
	JR	NC,WITH_RIGHT
PRINT_IN_LEFT
WITH_LEFT
	LD	A,(HL)
	AND	#0F
	LD	C,A
	LD	A,(DE)
	AND	#0F
	RLCA
	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